<?php

namespace App\Http\Controllers\Admin\Ocean;

use App\Common\Enums\StatusEnum;
use App\Common\Services\SystemApi\CenterApiService;
use App\Common\Tools\CustomException;
use App\Models\Ocean\OceanAccountModel;
use Illuminate\Http\Request;

class AccountController extends OceanController
{
    /**
     * @var string
     * 默认排序字段
     */
    protected $defaultOrderBy = 'account_id';

    /**
     * constructor.
     */
    public function __construct()
    {
        $this->model = new OceanAccountModel();

        parent::__construct();
    }

    /**
     * 分页列表预处理
     */
    public function selectPrepare(){
        parent::selectPrepare();

        $this->curdService->selectQueryBefore(function(){
            $this->curdService->customBuilder(function($builder){
                $roiCallbackStatus = $this->curdService->requestData['roi_callback_status'] ?? '';
                $builder->when($roiCallbackStatus,function ($query,$roiCallbackStatus){
                    return $query->where('roi_callback_status',$roiCallbackStatus);
                });
                $builder->where('parent_id', '<>', 0);
            });
        });

        $this->curdService->selectQueryAfter(function(){
            // 获取管理员列表
            $centerApiService = new CenterApiService();
            $adminUsers = $centerApiService->apiGetAdminUsers();

            // 映射
            $adminUserMap = array_column($adminUsers, null, 'id');

            foreach($this->curdService->responseData['list'] as $k => $v){
                $this->curdService->responseData['list'][$k]['admin_name'] = isset($adminUserMap[$v->admin_id]) ? $adminUserMap[$v->admin_id]['name'] : '';
            }
        });
    }

    /**
     * 列表预处理
     */
    public function getPrepare(){
        parent::getPrepare();

        $this->curdService->getQueryBefore(function(){
            $this->filter();
        });
    }




    /**
     * @param Request $request
     * @return mixed
     * @throws CustomException
     * 批量更新管理员
     */
    public function batchUpdateAdmin(Request $request){
        $requestData = $request->post();
        $this->validRule($requestData, [
            'ids' => 'required|array',
            'admin_id' => 'required',
        ]);

        $oceanAccountModel = new OceanAccountModel();
        $builder = $oceanAccountModel->whereIn('id', $requestData['ids']);

        if($builder->count() == 0){
            throw new CustomException([
                'code' => 'NOT_FOUND_ACCOUNT',
                'message' => '找不到对应账户',
            ]);
        }

        $builder->update(['admin_id' => $requestData['admin_id']]);

        return $this->success();
    }





    /**
     * @param Request $request
     * @return mixed
     *  启用 roi回传
     */
    public function roiCallbackEnable(Request $request){
        return $this->updateRoiCallbackStatus($request,StatusEnum::ENABLE);
    }


    /**
     * @param Request $request
     * @return mixed
     *  启用 roi回传
     */
    public function roiCallbackDisable(Request $request){
        return $this->updateRoiCallbackStatus($request,StatusEnum::DISABLE);
    }


    /**
     * @param Request $request
     * @param $status
     * @return mixed
     * 更新 roi回传状态
     */
    public function updateRoiCallbackStatus(Request $request,$status){
        $requestData = $request->all();
        // 查找
        $model = $this->model->find($requestData['id']);

        $model->roi_callback_status = $status;

        $ret = $model->save();

        // 清除 model data
        $ret && $this->clearModelData($requestData);

        return $this->ret($ret);
    }
}
